package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.RecommendUserApi;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.TodayBest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecommendUserService {
    /**
     * @负责与dobbo服务器进行交互
     */
//远程注入
    @Reference(version = "1.0.0")
    public RecommendUserApi recommendUserApi;

    public TodayBest queryTodayBest(long id) {
        RecommendUser recommendUser = this.recommendUserApi.queryWithMaxScore(id);
        if (recommendUser == null) {
            //没查出数据返回空 在调用出设置默认数据
            return null;
        }
        //有数据时 构建一个todayBest 装缘分值最高的用户id和缘分值
        TodayBest todayBest = new TodayBest();
        todayBest.setId(recommendUser.getUserId());
        //对缘分值向下去整
        double score = Math.floor(recommendUser.getScore());
        todayBest.setFateValue(Double.valueOf(score).longValue());
        return todayBest;

    }

    public PageInfo<RecommendUser> queryRecommendUserList(long id, Integer page, Integer pagesize) {
        return this.recommendUserApi.queryPageInfo(id, page, pagesize);
    }

    public List<RecommendUser> queryCardList(long userId, Integer count) {
        return this.recommendUserApi.queryCardList(userId, count);
    }

    /**
     * 查询推荐好友的缘分值
     *
     * @param userId
     * @param toUserId
     * @return
     */
    public Double queryScore(Long userId, Long toUserId) {
        Double score = this.recommendUserApi.queryScore(userId, toUserId);
        if (ObjectUtil.isEmpty(score)) {
            return 98d;
        }
        return score;
    }
}
